//
//  DYYuleCell.swift
//  SheYangBigData
//
//  Created by 宋海胜 on 2021/1/4.
//  Copyright © 2021 叁拾叁. All rights reserved.
//

import UIKit

enum YuleType {
    case zhusu
    case jingqu
    case meishi
}

class DYYuleCell: SYBaseCell , UITableViewDelegate, UITableViewDataSource {
    
    var model: DYJingquDetailModel!
    var tableView: UITableView!
    var type: YuleType = .zhusu
    var bgView: UIView = {
        let v: UIView = UIView()
        v.backgroundColor = UIColor(hex: 0x367FF6)
        v.layer.cornerRadius = 2
        return v
    }()
    
    var sepView: UIView = {
        let v: UIView = UIView()
        v.backgroundColor = Constant.bgViewColor
        return v
    }()
    
    let nameLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x1E1E1E)
        v.font = 17.autoBoldFontSize()
        v.text = "吃住娱乐"
        return v
    }()
    
    let chakan: UIButton = {
        let v: UIButton  = UIButton()
        v.setTitle("查看更多 >", for: .normal)
        v.setTitleColor(UIColor(hex: 0x5B5B5B), for: .normal)
        v.titleLabel?.font = 14.autoFontSize()
        v.layer.cornerRadius = 13.5
        v.backgroundColor = UIColor.white
        return v
    }()
    var types: [UIButton] = []

    public func configureView(_ model: DYJingquDetailModel, _ index: Int) {
        self.model = model
        
        if let s = model.hotelEnumList {
            
            for (i, value) in s.enumerated() {
                let v: UIButton  = UIButton()
                v.setTitle("\(value.name ?? "")", for: .normal)
                v.titleLabel?.font = 14.autoFontSize()
                v.layer.cornerRadius = 13.5
                v.tag = 100 + i
                if i == 0 {
                    v.setTitleColor(.white, for: .normal)
                    v.backgroundColor = UIColor(hex: 0x367FF6)
                } else {
                    v.setTitleColor(UIColor(hex: 0x1E1E1E), for: .normal)
                    v.backgroundColor = UIColor(hex: 0xF5F5F5)
                }
                v.addTarget(self, action: #selector(jiudianAction), for: .touchUpInside)
                contentView.addSubview(v)
                v.snp.makeConstraints { (make) in
                    make.top.equalTo(nameLb.snp.bottom).offset(15.0.autoWidth())
                    make.left.equalTo(bgView.snp.left).offset(75.0.autoWidth()*CGFloat(i))
                    make.width.equalTo(65.0.autoWidth())
                    make.height.equalTo(27.0.autoWidth())
                }
                types.append(v)
            }
        }
        if let c = model.tourHotelVOList?.count, c < 2 {
            
            tableView.snp.remakeConstraints { (make) in
                make.top.equalToSuperview().offset(autoWidth(73.0))
                make.left.right.equalToSuperview()
                make.height.equalTo(132.0.autoWidth())
            }
            chakan.isHidden = true
            chakan.snp.remakeConstraints { (make) in
                make.top.equalTo(tableView.snp.bottom)
                make.left.right.equalToSuperview()
                make.height.equalTo(0.autoWidth())
            }
        }
        self.tableView.reloadData()
    }
    
    override func initViews() {
        
        chakan.addTarget(self, action: #selector(chakanAction), for: .touchUpInside)
        setTableView()
        contentView.addSubview(nameLb)
        contentView.addSubview(bgView)
        
        contentView.addSubview(chakan)
        contentView.addSubview(tableView)
        contentView.addSubview(chakan)
        contentView.addSubview(sepView)
        bgView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(16.0.autoWidth())
            make.left.equalToSuperview().offset(autoWidth(14.5))
            make.width.equalTo(4.0.autoWidth())
            make.height.equalTo(15.0.autoWidth())
        }
        nameLb.snp.makeConstraints { (make) in
            make.centerY.equalTo(bgView)
            make.left.equalTo(bgView.snp.right).offset(autoWidth(7.0))
            make.right.equalToSuperview().offset(autoWidth(-15.0))
            make.height.equalTo(16.0.autoWidth())
        }
        
        tableView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(autoWidth(73.0))
            make.left.right.equalToSuperview()
            make.height.equalTo(230.0.autoWidth())
        }
        chakan.snp.makeConstraints { (make) in
            make.top.equalTo(tableView.snp.bottom)
            make.left.right.equalToSuperview()
            make.height.equalTo(40.0.autoWidth())
        }
        sepView.snp.makeConstraints { (make) in
            make.top.equalTo(chakan.snp.bottom)
            make.left.right.equalToSuperview()
            make.height.equalTo(10.0.autoWidth())
            make.bottom.equalToSuperview()
        }
    }
    
    func setTableView() {
        
        //创建表格视图
        self.tableView = UITableView(frame: CGRect(x: 0, y: NAV_HEIGHT, width: SCREEN_WIDTH, height: SCREEN_HEIGHT-NAV_HEIGHT-BOTTOM_SAFE_HEIGHT), style:.plain)
        self.tableView.separatorStyle = .none
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.isScrollEnabled = false
        self.tableView!.register(DYYuleSubLanCell.self, forCellReuseIdentifier: "Cell")
        self.contentView.addSubview(self.tableView!)
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: DYYuleSubLanCell? = tableView.dequeueReusableCell(withIdentifier: "Cell") as? DYYuleSubLanCell
        cell?.selectionStyle = .none
        if let m = self.model, let r = m.tourHotelVOList?[indexPath.row] {
            
            cell?.configureView(r)
        }
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        switch self.type {
        case .zhusu:
            let vc = DYZhusuDetailViewController("1")
            self.currentVC.navigationController?.pushViewController(vc, animated: true)
        case .jingqu:
            let vc = DYJingquDetailViewController("1")
            self.currentVC.navigationController?.pushViewController(vc, animated: true)
        case .meishi:
            let vc = DYMeishiDetailViewController("1")
            self.currentVC.navigationController?.pushViewController(vc, animated: true)
        }
    }
    
    @objc func chakanAction() {
        
        switch self.type {
        case .zhusu:
            let vc = DYListViewController(params: [:])
            self.currentVC.navigationController?.pushViewController(vc, animated: true)
        case .jingqu:
            let vc = DYMSListViewController(params: [:])
            self.currentVC.navigationController?.pushViewController(vc, animated: true)
        case .meishi:
            let vc = DYZSListViewController(params: [:])
            self.currentVC.navigationController?.pushViewController(vc, animated: true)
        }
    }
    
    @objc func jiudianAction(bt: UIButton) {
        
        types.forEach { (bt) in
            bt.setTitleColor(UIColor(hex: 0x1E1E1E), for: .normal)
            bt.backgroundColor = UIColor(hex: 0xF5F5F5)
        }
        switch bt.tag - 100 {
        case 0:
            self.type = .zhusu
            types[0].setTitleColor(UIColor.white, for: .normal)
            types[0].backgroundColor = UIColor(hex: 0x367FF6)
            
        case 1:
            self.type = .jingqu
            types[1].setTitleColor(UIColor.white, for: .normal)
            types[1].backgroundColor = UIColor(hex: 0x367FF6)
            
        case 2:
            self.type = .meishi
            types[2].setTitleColor(UIColor.white, for: .normal)
            types[2].backgroundColor = UIColor(hex: 0x367FF6)
            
            
        default:
            break
        }
    }
    
}

